
// 'starter.controllers' is found in controllers.js
var app = angular.module('starter', ['ionic', 'starter.services', 'CoderYuan', 'ngSanitize', 'ionicLazyLoad'])

  .run(function ($ionicPlatform, $rootScope, Storage, $state) {
    $ionicPlatform.ready(function () {
      if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        cordova.plugins.Keyboard.disableScroll(true);

      }
      if (window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleDefault();
      }
    });

    // 初始化全局变量
    $rootScope.globalInfo = {
      user: (function () {
        return Storage.get('user') || { ids: '' }
      })(), //全局user的数据
      noticeNum: ''
    }

    var exit = false;
    $ionicPlatform.registerBackButtonAction(function (e) {
      if ($location.path() == '/tab/privateTeach' || $location.path() == '/my/passwordEnter' || $location.path() == '/tab/league' || $location.path() == '/tab/venueDetails' || $location.path() == '/tab/appointment') {
        if (exit) {
          ionic.Platform.exitApp();
        } else {
          exit = true;
          // Message.show('再按一次退出系统', "500");
          setTimeout(function () {
            exit = false;
          }, 3000);
        }
      } else if ($ionicHistory.backView()) {
        $ionicHistory.goBack();
      } else {
        if (exit) {
          ionic.Platform.exitApp();
        }
        exit = true;
        setTimeout(function () {
          exit = false;
        }, 3000);
      }
      e.preventDefault();
      return false;
    }, 101);
  })
  .config(function ($ionicConfigProvider) {
    $ionicConfigProvider.platform.ios.tabs.style('standard');
    $ionicConfigProvider.platform.ios.tabs.position('bottom');
    $ionicConfigProvider.platform.android.tabs.style('standard');
    $ionicConfigProvider.platform.android.tabs.position('standard');

    $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
    $ionicConfigProvider.platform.android.navBar.alignTitle('left');


    $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
    //     $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-chevron-left');
    $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');
    //     $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-chevron-left');

    $ionicConfigProvider.platform.ios.views.transition('ios');
    $ionicConfigProvider.platform.android.views.transition('android');
  })

  .config(function ($stateProvider, $urlRouterProvider) {

    $stateProvider
      // 入口文件加载底部 tabs
      .state('tab', {
        url: '/tab',
        cache: false,
        abstract: true,
        templateUrl: 'templates/tabs.html'
      })

      // 团课
      .state('tab.league', {
        url: '/league',
        // cache: false,
        views: {
          'tab-league': {
            templateUrl: 'templates/tab-league.html',
            controller: 'leagueCtrl'
          }
        }
      })
      // 团课详情
      // 状态 
      .state('tab.details', {
        url: '/league/:chatId/:date',
        cache: false,
        views: {
          'tab-league': {
            templateUrl: 'templates/league/league-details.html',
            controller: 'detailsCtrl'
          }
        }
      })
      // 场馆详情
      .state('tab.venueDetails', {
        url: '/league/:chatId/:date/venueDetails',
        views: {
          'tab-league': {
            templateUrl: 'templates/league/venueDetails.html',
            controller: 'venueDetailsCtrl'
          }
        }
      })
      //场馆地图位置
      .state('tab.map', {
        url: '/league/:chatId/:date/map',
        views: {
          'tab-league': {
            templateUrl: 'templates/league/map.html',
            controller: 'mapCtrl'
          }
        }
      })
      // 教练详情
      .state('tab.coach', {
        url: '/league/:chatId/:date/coach',
        views: {
          'tab-league': {
            templateUrl: 'templates/league/coach.html',
            controller: 'coachCtrl'
          }
        }
      })
      // 预约
      .state('tab.appointment', {
        url: '/league/:chatId/:date/appointment',
        cache: false,
        views: {
          'tab-league': {
            templateUrl: 'templates/league/appointment.html',
            controller: 'appointmentCtrl'
          }
        }
      })
      // 完成预约
      .state('tab.complete', {
        url: '/league/:chatId/:date/complete',
        views: {
          'tab-league': {
            templateUrl: 'templates/league/complete.html',
            controller: 'completeCtrl'
          }
        }
      })
      // 私教
      .state('tab.privateTeach', {
        url: '/privateTeach',
        // cache: false,
        views: {
          'tab-privateTeach': {
            templateUrl: 'templates/tab-privateTeach.html',
            controller: 'privateTeachCtrl'
          }
        }
      })
      // 私教子页面
      .state('tab.privateTeachChats-detail', {
        url: '/privateTeach/:chatId',
        // cache:false,
        views: {
          'tab-privateTeach': {
            templateUrl: 'templates/privateTeach/privateTeachChats-detail.html',
            controller: 'privateTeachChatsCtrl'
          }
        }
      })
      // 私教地图
      .state('tab.privateTeachChats-map', {
        url: '/privateTeach/:chatId/map',
        views: {
          'tab-privateTeach': {
            templateUrl: 'templates/privateTeach/map.html',
            controller: 'privateTeachMapCtrl'
          }
        }
      })
      //私课 产品课程
      .state('tab.productCourse', {
        url: '/privateTeach/:chatId/:date/productCourse',
        cache: false,
        views: {
          'tab-privateTeach': {
            templateUrl: 'templates/privateTeach/productCourse.html',
            controller: 'productCourseCtrl'
          }
        }
      })
      // 私课 选择时间
      .state('tab.selectTime', {
        url: '/privateTeach/:chatId/:date/selectTime',
        cache: false,
        views: {
          'tab-privateTeach': {
            templateUrl: 'templates/privateTeach/selectTime.html',
            controller: 'selectTimeCtrl'
          }
        }
      })
      // 私课 完成
      // 完成预约
      .state('tab.completes', {
        url: '/privateTeach/completes',
        // cache: false,
        views: {
          'tab-privateTeach': {
            templateUrl: 'templates/privateTeach/completes.html',
            controller: 'completesCtrl'
          }
        }
      })
      // 课程
      .state('tab.course', {
        url: '/course',
        cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/tab-course.html',
            controller: 'courseCtrl'
          }
        }
      })
      // 全部订单
      .state('tab.allOrders', {
        url: '/course/allOrders',
        // cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/allOrders.html',
            controller: 'allOrdersCtrl'
          }
        }
      })
      // 待使用列表页面
      .state('tab.unused', {
        url: '/course/unused',
        // cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/unused.html',
            controller: 'unusedCtrl'
          }
        }
      })
      // 课程待使用详情
      .state('tab.unusedDetail', {
        url: '/course/unusedDetail/:chatId',
        cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/unusedDetail.html',
            controller: 'unusedDetailCtrl'
          }
        }
      })

      // 取消预约页面
      .state('tab.cancelOrder', {
        url: '/course/cancelOrder',
        cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/cancelOrder.html',
            controller: 'cancelOrderCtrl'
          }
        }
      })
      // 已取消列表页面
      .state('tab.cancelCourse', {
        url: '/course/cancelCourse',
        // cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/cancelCourse.html',
            controller: 'cancelCourseCtrl'
          }
        }
      })

      /*------课程中团课详情----------*/
      //课程团课信息
      // 团课详情
      // 状态
      .state('tab.leagueCourseDetail', {
        url: '/course/leagueCourseDetail/:chatId/:date',
        cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/leagueCourseDetail/leagueCourseDetail.html',
            controller: 'leagueCourseDetailCtrl'
          }
        }
      })

      // 场馆详情
      .state('tab.leagueCourseVenueDetail', {
        url: '/course/:chatId/:date/leagueCourseVenueDetail',
        // cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/leagueCourseDetail/venueDetails.html',
            controller: 'leagueCourseVenueDetailCtrl'
          }
        }
      })
      //场馆地图位置
      .state('tab.leagueCourseMap', {
        url: '/course/:chatId/:date/leagueCourseMap',
        // cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/leagueCourseDetail/map.html',
            controller: 'leagueCourseMapCtrl'
          }
        }
      })
      // 教练详情
      .state('tab.leagueCourseCoach', {
        url: '/course/:chatId/:date/leagueCourseCoach',
        // cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/leagueCourseDetail/coach.html',
            controller: 'leagueCourseCoachCtrl'
          }
        }
      })
      /*----------------*/

      /*----课程私课私课详情详情---*/
      .state('tab.privateCourseDetails', {
        url: '/course/privateCourseDetails/:chatId',
        cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/privateCourseDetail/privateCourseDetails.html',
            controller: 'privateCourseDetailsCtrl'
          }
        }
      })
      //场馆地图位置
      .state('tab.privateCourseMap', {
        url: '/course/:chatId/privateCourseMap',
        // cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/privateCourseDetail/map.html',
            controller: 'privateCourseMapCtrl'
          }
        }
      })
      //产品课程
      .state('tab.privateProductCourse', {
        url: '/course/:chatId/:date/privateProductCourse',
        cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/privateCourseDetail/productCourse.html',
            controller: 'privateProductCourseCtrl'
          }
        }
      })
      /*----------------*/
      // 课程取消详情页面
      .state('tab.cancelCourseDetail', {
        url: '/course/cancelCourseDetail/:chatId',
        cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/cancelCourseDetail.html',
            controller: 'cancelCourseDetailCtrl'
          }
        }
      })
      // 待评论列表页面
      // .state('tab.futureEvaluate', {
      //   url: '/course/futureEvaluate',
      //   // cache: false,
      //   views: {
      //     'tab-course': {
      //       templateUrl: 'templates/course/futureEvaluate.html',
      //       controller: 'futureEvaluateCtrl'
      //     }
      //   }
      // })
      // // 课程待评价详情
      // .state('tab.futureEvaluateDetail', {
      //   url: '/course/futureEvaluateDetail/:chatId',
      //   // cache: false,
      //   views: {
      //     'tab-course': {
      //       templateUrl: 'templates/course/futureEvaluateDetail.html',
      //       controller: 'futureEvaluateDetailCtrl'
      //     }
      //   }
      // })
      // 课程填写评价
      // .state('tab.writeEvaluate', {
      //   url: '/course/writeEvaluate',
      //   // cache: false,
      //   views: {
      //     'tab-course': {
      //       templateUrl: 'templates/course/writeEvaluate.html',
      //       controller: 'writeEvaluateCtrl'
      //     }
      //   }
      // })
      // 课程完成详情页面
      .state('tab.evaluateCompleteDetail', {
        url: '/course/evaluateCompleteDetail/:chatId',
        // cache: false,
        views: {
          'tab-course': {
            templateUrl: 'templates/course/evaluateCompleteDetail.html',
            controller: 'evaluateCompleteDetailCtrl'
          }
        }
      })

      //用户中心
      .state('tab.my', {
        url: '/my',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/tab-my.html',
            controller: 'myCtrl'
          }
        }
      })
      //会员卡列表
      .state('tab.membershipCardDetails', {
        url: '/my/membershipCardDetails',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/membershipCardDetails.html',
            controller: 'membershipCardDetailsCodeCtrl'
          }
        }
      })
      //会员卡详情
      .state('tab.membershipCardDetailsB', {
        url: '/my/membershipCardDetailsB/:chatId',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/membershipCardDetails/membershipCardDetailsw.html',
            controller: 'membershipbCodeCtrl'
          }
        }
      })
      //购买私课
      .state("tab.privateLessonsPurchased", {
        url: '/my/privateLessonsPurchased',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/privateLessonsPurchased.html',
            controller: 'privateLessonsPurchasedCtrl'
          }
        }
      })
      //my私教约课
      .state("tab.privateLessonsA", {
        url: '/my/privateTeachChatsDetailA/:chatId',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/privateTeach/privateTeachChats-detail.html',
            controller: 'privateTeachChatsDetailA'
          }
        }
      })
      //my私教课程产品
      .state("tab.privateLessonsB", {
        url: '/my/productCourse/:chatId',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/privateTeach/productCourse.html',
            controller: 'productCourseBCtrl'
          }
        }
      })
      //my私教课程产品 
      .state("tab.privateLessonsD", {
        url: '/my/map/:chatId',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/privateTeach/map.html',
            controller: 'mapMyCtrl'
          }
        }
      })
      //my选择教练预约时间
      .state("tab.privateLessonsC", {
        url: '/my/selectTime/:chatId',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/privateTeach/selectTime.html',
            controller: 'selectTimeCCtrl'
          }
        }
      })
      //my预约课完成
      .state("tab.privateLessonsE", {
        url: '/my/completes/:chatId',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/privateTeach/completes.html',
            controller: 'completesECtrl'
          }
        }
      })
      //柜子列表
      .state("tab.locker", {
        url: '/my/locker',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/locker.html',
            controller: 'lockerCtrl'
          }
        }
      })
      //柜子详情
      .state("tab.lockerDetails", {
        url: '/my/lockerDetails/:id',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/locker/lockerDetails.html',
            controller: 'lockerDetailsCtrl'
          }
        }
      })
      //二维码
      .state('tab.myTowCode', {
        url: '/my/myTowCode',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/myTowCode.html',
            controller: 'myTowCodeCtrl'
          }
        }
      })
      //设置
      .state('tab.mySet', {
        url: '/my/mySet',
        // cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/mySet.html',
            controller: 'mySetCtrl'
          }
        }
      })
      //个人信息
      .state('tab.myMessage', {
        url: '/my/myMessage',
        cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/myMessage.html',
            controller: 'myMessageCtrl'
          }
        }
      })
      .state('tab.modifyNickname', {
        url: '/my/myMessage/modifyNickname',
        // cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/my/modifyNickname.html',
            controller: 'modifyNicknameCtrl'
          }
        }
      })

      // 验证码登录页面
      .state('tab.login', {
        url: '/my/login',
        // cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/login/login.html',
            controller: 'loginCtrl'
          }
        }
      })
      //密码登录页面
      .state('tab.passwordEnter', {
        url: '/my/passwordEnter',
        // cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/login/passwordEnter.html',
            controller: 'passwordEnterCtrl'
          }
        }
      })
      //注册页面
      .state('tab.register', {
        url: '/my/register',
        // cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/login/register.html',
            controller: 'registerCtrl'
          }
        }
      })
      //注册 选择公司
      .state('tab.registerA', {
        url: '/my/selectCompany',
        // cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/login/selectCompany.html',
            controller: 'selectCompanyCtrl'
          }
        }
      })
      //注册 选择场馆
      .state('tab.registerB', {
        url: '/my/chooseVenues/:id',
        // cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/login/chooseVenues.html',
            controller: 'chooseVenuesCtrl'
          }
        }
      })
      //修改密码页面
      .state('tab.changePassword', {
        url: '/my/changePassword',
        // cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/login/changePassword.html',
            controller: 'changePasswordCtrl'
          }
        }
      })
      //忘记密码页面
      .state('tab.forgetPassword', {
        url: '/my/forgetPassword',
        // cache: false,
        views: {
          'tab-my': {
            templateUrl: 'templates/login/forgetPassword.html',
            controller: 'forgetPasswordCtrl'
          }
        }
      })
    $urlRouterProvider.otherwise('/tab/my/passwordEnter')
  })
app.directive('hideTabs', function ($rootScope) {
  return {
    restrict: 'A',
    link: function (scope, element, attributes) {
      scope.$on('$ionicView.beforeEnter', function () {
        scope.$watch(attributes.hideTabs, function (value) {
          $rootScope.hideTabs = value;
        });
      });

      scope.$on('$ionicView.beforeLeave', function () {
        $rootScope.hideTabs = false;
      });
    }
  };
});

//获取验证码
app.directive('timerbutton', function ($timeout, $interval) {
  return {
    restrict: 'AE',
    scope: {
      showTimer: '=',
      timeout: '='
    },
    link: function (scope, element, attrs) {
      scope.timer = false;
      scope.timeout = 60000;
      scope.timerCount = scope.timeout / 1000;
      scope.text = "获取验证码";

      scope.onClick = function () {
        scope.showTimer = true;
        scope.timer = true;
        scope.text = "秒后重新获取";
        var counter = $interval(function () {
          scope.timerCount = scope.timerCount - 1;
        }, 1000);

        $timeout(function () {
          scope.text = "获取验证码";
          scope.timer = false;
          $interval.cancel(counter);
          scope.showTimer = false;
          scope.timerCount = scope.timeout / 1000;
        }, scope.timeout);
      }
    },
    template: '<button on-tap="onClick()" class="button button-small button-calm xgmm-btn" ng-disabled="timer"><span ng-if="showTimer">{{ timerCount }}</span>{{text}}</button>'
  };
})

//服务模块
var services = angular.module("services", ['ionic']);

//控制器模块
var controllers = angular.module("starter.controllers", ['services']);

